package com.jy.adscm.zltx.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.bean.system.JyDept;
import com.jy.adscm.bean.system.JyUser;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.system.mapper.mysql.JyDeptMapper;
import com.jy.adscm.system.mapper.mysql.JyUserMapper;
import com.jy.adscm.zltx.bean.*;
import com.jy.adscm.zltx.mapper.mysql.*;
import com.jy.adscm.zltx.service.QsApprovalRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zyl
 * @since 2018-11-02
 */
@Service
public class QsApprovalRecordServiceImpl extends ServiceImpl<QsApprovalRecordMapper, QsApprovalRecord> implements QsApprovalRecordService {
   @Autowired
    QsApprovalRecordMapper qsApprovalRecordMapper;
   @Autowired
    QsApplyUpdateRecordMapper qsApplyUpdateRecordMapper;
   @Autowired
    QsFileContentMapper QsFileContentMapper;
   @Autowired
    QsUpdatedFileTrainMapper QsUpdatedFileTrainMapper;
   @Autowired
    JyUserMapper jyUserMapper;
   @Autowired
    JyDeptMapper jyDeptMapper;
   @Autowired
    QsFileMapper QsFileMapper;
    @Override
    public List<QsApprovalRecord> selectByUpdateId(Integer updateId,Integer deptId) throws AdscmSystemException{
        Wrapper<QsApprovalRecord> wrapper =new EntityWrapper<>();
        wrapper.eq("updateId",updateId);
        List<QsApprovalRecord> qsApprovalRecords = qsApprovalRecordMapper.selectList(wrapper);
        if (qsApprovalRecords.size()>0){
            for (QsApprovalRecord qsApprovalRecord : qsApprovalRecords) {
                 if (qsApprovalRecord.getApprovaler()!=null&&!"".equals(qsApprovalRecord.getApprovaler())){
                     JyUser jyUser = jyUserMapper.selectById(qsApprovalRecord.getApprovaler());
                     qsApprovalRecord.setApprovalerName(jyUser.getName());
                 }
                 if (qsApprovalRecord.getDeptId()!=null&&!"".equals(qsApprovalRecord.getDeptId())){
                     JyDept jyDept = jyDeptMapper.selectById(qsApprovalRecord.getDeptId());
                     qsApprovalRecord.setDeptName(jyDept.getDeptName());
                 }
            }
        }
        return qsApprovalRecords;
    }
    @Override
    @Transactional
    public  boolean updateQsApprovalRecord(QsApprovalRecord qsApprovalRecord) throws AdscmSystemException{
        //先进行修改
        boolean result = super.updateById(qsApprovalRecord);

        //查出所有的审核记录是否已经通过审核
        Wrapper<QsApprovalRecord> wrapper = new EntityWrapper<>();
        wrapper.eq("updateId",qsApprovalRecord.getUpdateId());
        List<QsApprovalRecord> qsApprovalRecords = qsApprovalRecordMapper.selectList(wrapper);
        int temp=0;
        for (QsApprovalRecord approvalRecord : qsApprovalRecords) {
            if (approvalRecord.getApprovalTime()==null){
                break;
            }
            if ("0".equals(approvalRecord.getIsApproval())){
                QsApplyUpdateRecord qsApplyUpdateRecord = qsApplyUpdateRecordMapper.selectById(qsApprovalRecord.getUpdateId());
                qsApplyUpdateRecord.setApprovalResult("0");
                qsApplyUpdateRecord.setIsFinishUpdate("-1");
                qsApplyUpdateRecordMapper.updateById(qsApplyUpdateRecord);
                break;
            }
          temp++;
        }
        if (temp==qsApprovalRecords.size()){
            QsApplyUpdateRecord qsApplyUpdateRecord = qsApplyUpdateRecordMapper.selectById(qsApprovalRecord.getUpdateId());
            qsApplyUpdateRecord.setIsFinishUpdate("0");
            qsApplyUpdateRecord.setApprovalResult("1");
            qsApplyUpdateRecordMapper.updateById(qsApplyUpdateRecord);
            //完成审核通过，根据审核通过后 先根据申请单上是否要进行培训生成培训记录
            if("1".equals(qsApplyUpdateRecord.getIsTrain())){
                QsUpdatedFileTrain qsUpdatedFileTrain=new  QsUpdatedFileTrain();
                qsUpdatedFileTrain.setVersionCode(qsApplyUpdateRecord.getVersionCode());
                qsUpdatedFileTrain.setFileId(qsApplyUpdateRecord.getFileId());
                QsUpdatedFileTrainMapper.insert(qsUpdatedFileTrain);
            }
            //开启该文件下的限制，变成可以修改 查询当前fileId 和isUse是1的
            Wrapper<QsFileContent> wrapper1= new EntityWrapper<>();
            wrapper1.eq("fileId",qsApprovalRecord.getFileId());
            wrapper1.eq("isUse","1");
            List<QsFileContent> qsFileContents = QsFileContentMapper.selectList(wrapper1);
            for (QsFileContent qsFileContent : qsFileContents) {
                qsFileContent.setIsLocked(0);
                QsFileContentMapper.updateByQsFileContent(qsFileContent);
            }
            //把当前版本修改成未发布并且把当前文件编号和文件版本号在原有的文件信息上进行更新

            QsFile qsFile = QsFileMapper.selectById(qsApprovalRecord.getFileId());
            if (qsFile!=null){
                qsFile.setIsApproval("0");
                qsFile.setFileCode(qsApplyUpdateRecord.getFileCode());
                qsFile.setVersionCode(qsApplyUpdateRecord.getVersionCode());
                QsFileMapper.updateById(qsFile);
            }else {
                throw    new AdscmSystemException("未找到文件基本信息");
            }
        }



        return result;
    }
}
